<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Hint;
use App\Lib\Response;
use App\Http\Model\FinanceRechargeOrder;
use App\Http\Services\FinanceRechargeOrderServices;
use App\Http\Services\ThinkingdataServices;

class CeshiController extends BaseController
{
    private $shumeiFilterConf;
    protected $rechargeOrderServices;

    public function __construct(FinanceRechargeOrderServices $rechargeOrderServices)
    {
        $this->rechargeOrderServices = $rechargeOrderServices;

    }

    /**
     * 设置验证码
     */
    public function saveSmsCode(Request $request)
    {
        $phone = $request->get('phone');
        if(empty($phone)) {
            Response::returnResult(Hint::PARAM_ERROR, [], '', 0);
        }

        //redis 存储
        $redisKey = 'sms_'.$phone;
        $this->redisCache($redisKey, 123456, 3600);
        Response::returnResult(Hint::SUCCESS, [], '', 0);
    }
    //redis存储
    private function redisCache($key,$value,$expire_time = 300){
        Redis::setex($key,$expire_time,$value);
        return true;
    }

    /**
     * 数美
     */
    public function toDo(){
        $this->shumeiFilterConf = config('shumei_filter');
        $res = $this->checkShumeiContent('值');

        Response::returnResult(Hint::SUCCESS, [$res], '', 0);
    }

    public function checkShumeiContent($content){
        $url = $this->shumeiFilterConf['apihost'];
        $data['accessKey'] = $this->shumeiFilterConf['accessKey'];
        $data['type'] = $this->shumeiFilterConf['type'];
        $data['appId'] = $this->shumeiFilterConf['appId'];
        $data['organization'] = $this->shumeiFilterConf['organization'];
        $data['data'] = [
            "text" => $content,
            "tokenId" => (string)$this->getDataId()
        ];

        $ret_data = curl_request($url, $data, 'json', 'POST');
        $ret_data = json_decode($ret_data, true);
        return $ret_data;

        if ($ret_data['code'] == 1100 && $ret_data['riskLevel'] && $ret_data['riskLevel'] == "PASS") {

        } else {

        }
        return false;
    }
    private function getDataId()
    {
        return uniqid();
    }


    /**
     * 设置一个订单并支付成功
     */
    public function makeOrder(Request $request)
    {
        $user_id = $request->get('user_id');
        $price = (int)$request->get('price');
        if(empty($user_id) || empty($price)) {
            Response::returnResult(Hint::PARAM_ERROR, [], '', 0);
        }
        //创建订单
        $rechargeOrder = new FinanceRechargeOrder();
        $order_sn = create_recharge_order_sn();
        $diamondAmount = intval($price/10);
        $orderId = $rechargeOrder->create_order($order_sn, $user_id, 0, $diamondAmount, $price, 1, 404, 0, 0);
        //支付成功
        $res = $this->rechargeOrderServices->notifyOp($order_sn, $order_sn);

        Response::returnResult(Hint::SUCCESS, [$orderId, $res], '', 0);
    }


    /**
     * 数数上报
     */
    public function tdSend(Request $request)
    {
        $serveice = new ThinkingdataServices();

        $type = 'login';
        $data = '';
        $serveice->handleTrack($type, $data);
    }







}
